<?php
namespace app\controller;

use think\Request;
use think\facade\Db;

class Sys extends Common
{

	/**
	 * 安装代码
	 *
	 * @return
	 */
	public function info(Request $request)
	{
		return json(['code'=>0, 'msg'=>'操作成功', 'companyId'=> '<script type="text/javascript">
(function() {
	var createCloud = document.createElement("script");
	createCloud.id = "san_cloud";
	createCloud.setAttribute("sid", "'.$request->sessInfo['companyId'].'");
	createCloud.src = "https://api.jiafen520.com/cloud/san_cloud.js";
	var createAgl = document.getElementsByTagName("script")[0];
	createAgl.parentNode.insertBefore(createCloud, createAgl);
})();
</script>' ]);
	}

	/**
	 * 站点配置
	 *
	 * @return
	 */
	public function site(Request $request)
	{
		if($request->isPut()) {
			$array = [];
			$filename = root_path('data') . md5($request->sessInfo['companyId'] . 'site' );
			$array['sitename'] = $request->param('sitename', '', 'trim');
			$array['siteversion'] = $request->param('siteversion', '', 'trim');
			$array['sitestatistics'] = $request->param('sitestatistics', '', 'trim');
			file_put_contents($filename, serialize($array));
			return json(['code'=>0, 'msg'=>'操作成功', 'data'=> [] ]);
		}

		if($request->isGet()) {

			$filename = root_path('data') . md5($request->sessInfo['companyId'] . 'site' );
			$siteArray = file_get_contents($filename);
			if($siteArray){
				$siteArray = unserialize($siteArray);
			}

			$filename = root_path('data') . md5($request->sessInfo['companyId'] . 'ocpc' );
			$ocpcArray = file_get_contents($filename);
			if($ocpcArray){
				$ocpcArray = unserialize($ocpcArray);
			}

			$filename = root_path('data') . md5($request->sessInfo['companyId'] . 'spider' );
			$spiderArray = file_get_contents($filename);
			if($spiderArray) {
				$spiderArray = unserialize($spiderArray);
			}

			return json(['code'=>0, 'msg'=>'操作成功', 'data'=> [ 'site' => $siteArray, 'ocpc' => $ocpcArray, 'spider' => $spiderArray  ] ]);
		}
		return json(['code'=>0, 'msg'=>'操作成功', 'data'=> [] ]);
	}

	/**
	 * OCPC配置
	 *
	 * @return
	 */
	public function ocpc(Request $request)
	{
		$array = [];
		if($request->isPut()) {
			$filename = root_path('data') . md5($request->sessInfo['companyId'] . 'ocpc' );
			$array['baiduocpc'] = $request->param('baiduocpc', '', 'trim');
			$array['baiduexocpc'] = $request->param('baiduexocpc', '', 'trim');
			file_put_contents($filename, serialize($array));
			$query = Db::table('cfg_system')->where('companyId', $request->sessInfo['companyId'])->value('id');
			if( $query ) {
				Db::name('cfg_system')->where('id', $query)->update($array);
			} else {
				Db::name('cfg_system')->insert(['companyId' => $request->sessInfo['companyId'], 'baiduocpc' => $array['baiduocpc'], 'baiduexocpc' => $array['baiduexocpc'] ]);
			}
		}
		return json(['code'=>0, 'msg'=>'操作成功', 'data'=> [] ]);
	}

	/**
	 * 拦截IP配置
	 *
	 * @return
	 */
	public function spider(Request $request)
	{
		$array = [];
		if($request->isPut()) {
			$filename = root_path('data') . md5($request->sessInfo['companyId'] . 'spider' );
			$array['interceptingip'] = $request->param('interceptingip', '', 'trim');
			file_put_contents($filename, serialize($array));
		}
		return json(['code'=>0, 'msg'=>'操作成功', 'data'=> [] ]);
	}

	/**
	 * 后台菜单
	 *
	 * @return
	 */
	public function menu(Request $request)
	{
		if( $request->sessInfo['level'] > 1) {
			return ['code'=> 403, 'msg'=> '你没有权限执行此操作!'];;
		}

		if( $request->isDelete() ) {
			$param = $request->param();
			if( isset($param['id']) ){
				$beginTime = microtime(true);
				$title = Db::table('cfg_menu')->where('menuId', $param['id'])->value('title');
				Db::table('cfg_menu')->where('menuId', $param['id'])->delete();
				$endTime = microtime(true);
				$this->cfg_write_oper($request, $request->sessInfo['token'], $request->sessInfo['companyId'], $request->sessInfo['nickname'],
					$request->sessInfo['username'], round($endTime - $beginTime, 3), '菜单管理', '删除['.$title.']', 0, json_encode($param));
			}
			return json(['code'=> 0, 'msg'=> '删除成功!']);
		}

		if($request->isPost()) {
			$authority = $request->param('authority', '', 'trim');
			$component = $request->param('component', '', 'trim');
			$hide = $request->param('hide', '', 'trim');
			$icon = $request->param('icon', '', 'trim');
			$menuType = $request->param('menuType', '', 'trim');
			$openType = $request->param('openType', '', 'trim');
			$parentId = $request->param('parentId', '', 'trim');
			$path = $request->param('path', '', 'trim');
			$sortNumber = $request->param('sortNumber', '', 'trim');
			$title = $request->param('title', '', 'trim');
			$menuLevel = $request->param('menuLevel', '', 'trim');
			if($parentId == 0){
				$component = null;
			}
			Db::name('cfg_menu')->insert(['authority' => $authority, 'component' => $component, 'hide' => $hide, 'icon' => $icon, 'menuLevel' => $menuLevel,
			'menuType' => $menuType, 'openType' => $openType, 'parentId' => $parentId, 'path' => $path, 'createTime' => date('Y-m-d H:i:s'),
			'sortNumber' => $sortNumber, 'title' => $title]);
			return json(['code'=>0, 'msg'=>'操作成功']);
		}

		if($request->isPut()) {
			$parentId = $request->param('parentId', '', 'trim');
			$authority = $request->param('authority', '', 'trim');
			$component = $request->param('component', '', 'trim');
			$hide = $request->param('hide', '', 'trim');
			$icon = $request->param('icon', '', 'trim');
			$menuId = $request->param('menuId', '', 'trim');
			$menuType = $request->param('menuType', '', 'trim');
			$openType = $request->param('openType', '', 'trim');
			$sortNumber = $request->param('sortNumber', '', 'trim');
			$title = $request->param('title', '', 'trim');
			$path = $request->param('path', '', 'trim');
			$menuLevel = $request->param('menuLevel', '', 'trim');
			$updateTime = date('Y-m-d H:i:s');
			if($menuId == $parentId) {
				return json(['code'=>403, 'msg'=>'上级菜单与当前菜单冲突!']);
			}
			if($parentId == 0) {
				$component = null;
			}
			Db::name('cfg_menu')
					->where('menuId', $menuId)
					->update([ 'authority' => $authority,
					'component' => $component, 'hide' => $hide, 'icon' => $icon, 'menuId' => $menuId, 'parentId' => $parentId, 'path' => $path,
					'menuType' => $menuType, 'openType' => $openType, 'sortNumber' => $sortNumber, 'title' => $title, 'menuLevel' => $menuLevel,
					'updateTime' => $updateTime]);
			return json(['code'=>0, 'msg'=>'操作成功']);
		}

		if($request->isGet()) {
			$data = Db::table('cfg_menu')->order('sortNumber', 'asc')->select()->toArray();
			return json(['code'=>0, 'data' => $data, 'msg'=>'操作成功']);
		}

		return json(['code'=> 403, 'msg'=> '非法提交你的ip已被记录!']);
	}

}
